﻿var geocoder = new google.maps.Geocoder();
var map = null;

var infoWindow = new google.maps.InfoWindow();
var markerBounds = new google.maps.LatLngBounds();
var markerArray = [];
function makeMarker(options) {
    var pushPin = new google.maps.Marker({ map: map });
    pushPin.setOptions(options);
    google.maps.event.addListener(pushPin, "click", function () {
        infoWindow.setOptions(options);
        infoWindow.open(map, pushPin);
    });
    markerBounds.extend(options.position);
    markerArray.push(pushPin);
    return pushPin;
}

function initialize(calle, numero, localidad, provincia, jsonVisitante) {
    var myJSON = JSON.decode(jsonVisitante);
    //obtiene una posicion
    geocoder.waitingDelay = setTimeout(function () {
        geocoder.geocode({ address: calle + ', ' + numero + ', ' + localidad + ', ' + provincia }, function geocodeResult(response, status) {
            if (status == google.maps.GeocoderStatus.OK && response[0]) {
                geocoder.firstItem = response[0];
                var len = response.length;
                for (var i = 0; i < len; i++) {
                    var latPos = response[i].geometry.location.lat();
                    var longPos = response[i].geometry.location.lng();
                    var latlng = new google.maps.LatLng(latPos, longPos);
                    //setea la opciones del mapa      
                    var myOptions = {
                        zoom: 17,
                        center: latlng,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    //genera el mapa
                    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                    //marca el mapa
                    makeMarker({
                        position: new google.maps.LatLng(latPos, longPos),
                        content: 'Nombre: ' + myJSON.FullName + ' <br/>Direccion: ' + myJSON.Direccion + ' ' + myJSON.Numero,
                        title: myJSON.FullName,
                        id: myJSON.Id
                    }, map);


                }
            } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
                //        document.getElementById("suggest_list").innerHTML = "?";
                //        geocoder.resultAddress = "";
                //        geocoder.resultBounds = null;
            } else {
                //        document.getElementById("suggest_list").innerHTML = status;
                //        geocoder.resultAddress = "";
                //        geocoder.resultBounds = null;
            }
        }
        );
    }, 300);

}


function marcarMapa(jsonRevisitas) {
    var jsonRvts = JSON.decode(jsonRevisitas);
    //obtiene una posicion
    jsonRvts.each(function (rvt) {
        geocoder.waitingDelay = setTimeout(function () {
            geocoder.geocode({ address: rvt.Calle + ', ' + rvt.Numero + ', ' + rvt.Localidad + ', ' + rvt.Provincia }, function geocodeResult(response, status) {
                if (status == google.maps.GeocoderStatus.OK && response[0]) {
                    geocoder.firstItem = response[0];
                    var len = response.length;
                    for (var i = 0; i < len; i++) {
                        var latPos = response[i].geometry.location.lat();
                        var longPos = response[i].geometry.location.lng();
                        var latlng = new google.maps.LatLng(latPos, longPos);
                        makeMarker({
                            position: new google.maps.LatLng(latPos, longPos),
                            content: 'Nombre: ' + rvt.Nombre + ' <br/>Direccion: ' + rvt.Calle + ' ' + rvt.Numero,
                            title: rvt.Nombre
                        }, map);
                    }
                } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
                    //        document.getElementById("suggest_list").innerHTML = "?";
                    //        geocoder.resultAddress = "";
                    //        geocoder.resultBounds = null;
                } else {
                    //        document.getElementById("suggest_list").innerHTML = status;
                    //        geocoder.resultAddress = "";
                    //        geocoder.resultBounds = null;
                }
            }
        );
        }, 300);
    });
}

function checkPosicion(calle, numero, localidad, provincia, jsonVisitante, nombre) {
   
    geocoder.waitingDelay = setTimeout(function () {
        geocoder.geocode({ address: calle + ', ' + numero + ', ' + localidad + ', ' + provincia }, function geocodeResult(response, status) {
            if (status == google.maps.GeocoderStatus.OK && response[0]) {
                geocoder.firstItem = response[0];
                var len = response.length;
                for (var i = 0; i < len; i++) {
                    var latPos = response[i].geometry.location.lat();
                    var longPos = response[i].geometry.location.lng();
                    var latlng = new google.maps.LatLng(latPos, longPos);
                    //setea la opciones del mapa      
                    var myOptions = {
                        zoom: 17,
                        center: latlng,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    //genera el mapa
                    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                    //marca el mapa
                    makeMarker({
                        position: new google.maps.LatLng(latPos, longPos),
                        content: 'Nombre: ' + nombre + ' <br/>Direccion: ' + calle + ' ' + numero,
                        title: nombre,
                   
                    }, map);


                }
            } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
                //        document.getElementById("suggest_list").innerHTML = "?";
                //        geocoder.resultAddress = "";
                //        geocoder.resultBounds = null;
            } else {
                //        document.getElementById("suggest_list").innerHTML = status;
                //        geocoder.resultAddress = "";
                //        geocoder.resultBounds = null;
            }
        }
        );
    }, 300);

}